﻿#include "detectiondlg.h"
#include "ui_detectiondlg.h"


#include <QMouseEvent>
#include <QDebug>
#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>
#include <QMessageBox>
#include <QTextCodec>
#include <QSettings>



DetectionDlg::DetectionDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DetectionDlg)
{
    ui->setupUi(this);
    this->move(0, 58);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);



    m_dRoi = new RoiDlg(this);
    m_dRoi->hide();

    // 设置事件触发
    ui->label->installEventFilter(this);
    ui->label->setMouseTracking(true);

    SetAllToolTip();


    // 定时器
    myTimer = new QTimer(this);
    QObject::connect(myTimer, SIGNAL(timeout()), this, SLOT(systimer()));
    myTimer->start(200);

}

DetectionDlg::~DetectionDlg()
{
    delete ui;
}


void DetectionDlg::systimer(){
    //qDebug() << "DetectionDlg  systimer";
//    if(m_nTest){
//        on_staticBt_clicked();
//        on_downBt_clicked();
//    }
}
void DetectionDlg::SetAllToolTip(){
    // 设置提示说明
    ui->text1_1->setToolTip(QString::fromLocal8Bit("换算比例: \n默认 0.0097189;\n实际尺寸与像素尺寸的比值 建议：不做任何更改；"));
    ui->text1_2->setToolTip(QString::fromLocal8Bit("内圆标准尺寸: \n默认 0.705;\n内值径标准值 建议：根据不同产品规格，进行更改；"));
    ui->text1_3->setToolTip(QString::fromLocal8Bit("内圆补偿值: \n默认 0;\n整体补偿内值径大小，补偿值越大，内径值变大,建议范围-0.02mm-0.02mm之间；"));
    ui->text1_4->setToolTip(QString::fromLocal8Bit("外圆标准尺寸: \n默认 2.24;\n外值径标准值 建议：根据不同产品规格，进行更改；"));
    ui->text1_5->setToolTip(QString::fromLocal8Bit("外圆补偿值: \n默认 0;\n整体补偿外径大小，补偿值越大，外径值变大,建议范围 -0.02mm-0.02mm之间；"));
    ui->text1_6->setToolTip(QString::fromLocal8Bit("内圆上限标准偏差: \n默认 0.015;\n内直径上限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text1_7->setToolTip(QString::fromLocal8Bit("内圆下限标准偏差: \n默认 0.005;\n内直径下限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text1_8->setToolTip(QString::fromLocal8Bit("外圆上限标准偏差: \n默认 0.02;\n外直径上限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text1_9->setToolTip(QString::fromLocal8Bit("外圆下限标准偏差: \n默认 0.;\n外直径下限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text1_10->setToolTip(QString::fromLocal8Bit("同心距上限偏差: \n默认 0.025;\n同心距上限值 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text1_11->setToolTip(QString::fromLocal8Bit("内圆度标准1: \n默认 0.8;\n内圆度标准1，算法粗略检测 建议范围0.7-0.9之间，结合参数内圆度标准2，内圆标准1＜ 内圆标准2；"));
    ui->text1_12->setToolTip(QString::fromLocal8Bit("内圆度标准2: \n默认 0.85;\n内圆圆度标准2，算法精确检测 建议范围0.8-0.99之间，结合内圆度标准1，内圆标准1＜ 内圆标准2；"));
    ui->text1_13->setToolTip(QString::fromLocal8Bit("外圆度标准1: \n默认 0.8;\n外圆圆度标准1，算法粗略检测 建议范围0.7-0.9之间，结合外圆圆度标准2，外圆标准1＜ 外圆标准2；"));
    ui->text1_14->setToolTip(QString::fromLocal8Bit("外圆度标准2: \n默认 0.85;\n外圆圆度标准2，算法粗略检测 建议范围0.8-0.99之间，结合外圆圆度标准1，外圆标准1＜ 外圆标准2；"));
    ui->text1_15->setToolTip(QString::fromLocal8Bit("外圆形面积: \n默认 150470;\n外圆像素面积 建议：不做任何更改；"));
    ui->text1_16->setToolTip(QString::fromLocal8Bit("内圆形面积: \n默认 16480;\n内圆像素面积 建议：不做任何更改；"));
    ui->text1_17->setToolTip(QString::fromLocal8Bit("存图: \n0   不存图\n1   存图；"));


    ui->text2_1->setToolTip(QString::fromLocal8Bit("换算比例: \n默认 0.00488566;\n实际尺寸与像素尺寸的比值 建议：不做任何更改；"));
    ui->text2_2->setToolTip(QString::fromLocal8Bit("标准尺寸: \n默认 7.05;\n高度标准值 建议：根据不同产品规格，进行更改；"));
    ui->text2_3->setToolTip(QString::fromLocal8Bit("尺寸补偿: \n默认 0;\n整体补偿高度，补偿值越大，高度值变大,建议范围-0.02mm-0.02mm之间；"));
    ui->text2_4->setToolTip(QString::fromLocal8Bit("尺寸上限偏差: \n默认 0;\n高度上限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text2_5->setToolTip(QString::fromLocal8Bit("尺寸下限偏差: \n默认 0.1;\n高度下限范围 建议(0mm-0.2mm)：根据不同产品型号规格，进行更改；"));
    ui->text2_6->setToolTip(QString::fromLocal8Bit("玻璃边坐标X: \n默认 1800;\n玻璃边焦平面的x坐标值 建议：不做任何更改；"));
    ui->text2_7->setToolTip(QString::fromLocal8Bit("玻璃边坐标Y: \n默认 1550;\n玻璃边焦平面的y坐标值 建议：不做任何更改；"));
    ui->text2_8->setToolTip(QString::fromLocal8Bit("测量边的长度: \n默认 80;\n测量边的长度 建议：不做任何更改；"));
    ui->text2_9->setToolTip(QString::fromLocal8Bit("存图: \n0   不存图\n1   存图；"));

    ui->text3_1->setToolTip(QString::fromLocal8Bit("外围变形面积: \n默认 50;\n主要检测严重变形的物体，有一个严重变形突出，越大检测越松；"));
    ui->text3_2->setToolTip(QString::fromLocal8Bit("外围内缩尺寸: \n默认  5;\n主要是根据图像识别的外圆直径，做一个外圆检测范围屏蔽，越大检测越松，一般外缩不要超过10；"));
    ui->text3_3->setToolTip(QString::fromLocal8Bit("外围检测区域: \n默认  5;\n主要是根据图像识别的外圆里面的第一个圆，往外做一个检测范围屏蔽，越大检测越松，一般不要超过10；"));
    ui->text3_4->setToolTip(QString::fromLocal8Bit("黑块阈值:    \n默认  5;\n主要检测端面严重变形或者凹坑缺陷，越大检测越严格，一般根据图像亮度调整，一般不要超过80；"));
    ui->text3_5->setToolTip(QString::fromLocal8Bit("黑块面积: \n默认  40;\n主要检测端面严重变形或者凹坑缺陷，越大检测越严格，一般根据图像亮度调整，一般不要超过80；"));
    ui->text3_6->setToolTip(QString::fromLocal8Bit("缺陷阈值: \n默认  8;\n主要检测分散的小缺陷，越大检测越严格；"));
    ui->text3_7->setToolTip(QString::fromLocal8Bit("缺陷面积: \n默认  350;\n主要是检测分散的小缺陷面积，越大检测越松；"));
    ui->text3_8->setToolTip(QString::fromLocal8Bit("外围缺陷阈值: \n默认  35;\n主要检测外围圆四周的缺损缺陷，结合参数2进行调整，越大检测越松，一般范围在25到50之间；"));
    ui->text3_9->setToolTip(QString::fromLocal8Bit("外围缺陷面积: \n默认  50;\n配合参数 外围缺陷阈值 ，缺陷面积，越大检测越松；"));
    ui->text3_10->setToolTip(QString::fromLocal8Bit("裂痕阈值: \n默认  2;\n主要检测裂痕，越大越严格，一般不要超过10；"));
    ui->text3_11->setToolTip(QString::fromLocal8Bit("裂痕长度: \n默认  15;\n配合参数 裂痕阈值 进行检测，为裂痕的长度，越大检测越松，一般范围在5到40；"));
    ui->text3_12->setToolTip(QString::fromLocal8Bit("涂层阈值: \n默认  180;\n主要检测涂层缺损，为涂层亮度的阈值，越大越松，一般范围在160到220之间；"));
    ui->text3_13->setToolTip(QString::fromLocal8Bit("涂层面积: \n默认  500;\n结合参数 涂层阈值 进行检测，越大越松；"));
    ui->text3_14->setToolTip(QString::fromLocal8Bit("内孔阈值: \n默认  180;\n定位内孔阈值；"));
    ui->text3_15->setToolTip(QString::fromLocal8Bit("内孔缺陷面积: \n默认  20;\n内孔异物的面积，越大检测越松；"));
    ui->text3_16->setToolTip(QString::fromLocal8Bit("内孔内缩尺寸: \n默认  2;\n根据内孔检测园的半径，外内内缩尺寸，越大检测越松；"));
    ui->text3_17->setToolTip(QString::fromLocal8Bit("内孔最小尺寸: \n默认  30;\n检测内孔园的半径，小于这个直接判别废品；"));
    ui->text3_18->setToolTip(QString::fromLocal8Bit("台阶检测区域位置: \n默认  10;\n检测内孔沉台的缺陷，为在内孔半径基础上外扩尺寸，该尺寸数据过大会定位到沉台黑色区域，引起误检，太小则会检测内孔不需要判别的异物；"));
    ui->text3_19->setToolTip(QString::fromLocal8Bit("台阶检测区域宽度: \n默认  8;\n在参数 台阶检测区域位置 的基础上，检测范围，越大越严格；"));
    ui->text3_20->setToolTip(QString::fromLocal8Bit("台阶检测阈值: \n默认  35;\n沉台异物的黑色阈值，越大越严格；"));
    ui->text3_21->setToolTip(QString::fromLocal8Bit("台阶缺陷面积: \n默认  50;\n配合参数20，缺陷面积，越大越松；"));
    ui->text3_22->setToolTip(QString::fromLocal8Bit("是否存带标识图: \n0   不存图\n1   存缺陷标识图\n2   存不带标识图"));


    ui->text4_1->setToolTip(QString::fromLocal8Bit("外围变形面积: \n默认 50;\n主要检测严重变形的物体，有一个严重变形突出，越大检测越松；"));
    ui->text4_2->setToolTip(QString::fromLocal8Bit("外围内缩尺寸: \n默认  5;\n主要是根据图像识别的外圆直径，做一个外圆检测范围屏蔽，越大检测越松，一般外缩不要超过10；"));
    ui->text4_3->setToolTip(QString::fromLocal8Bit("外围检测区域: \n默认  5;\n主要是根据图像识别的外圆里面的第一个圆，往外做一个检测范围屏蔽，越大检测越松，一般不要超过10；"));
    ui->text4_4->setToolTip(QString::fromLocal8Bit("黑块阈值:    \n默认  5;\n主要检测端面严重变形或者凹坑缺陷，越大检测越严格，一般根据图像亮度调整，一般不要超过80；"));
    ui->text4_5->setToolTip(QString::fromLocal8Bit("黑块面积: \n默认  40;\n主要检测端面严重变形或者凹坑缺陷，越大检测越严格，一般根据图像亮度调整，一般不要超过80；"));
    ui->text4_6->setToolTip(QString::fromLocal8Bit("缺陷阈值: \n默认  8;\n主要检测分散的小缺陷，越大检测越严格；"));
    ui->text4_7->setToolTip(QString::fromLocal8Bit("缺陷面积: \n默认  350;\n主要是检测分散的小缺陷面积，越大检测越松；"));
    ui->text4_8->setToolTip(QString::fromLocal8Bit("外围缺陷阈值: \n默认  35;\n主要检测外围圆四周的缺损缺陷，结合参数2进行调整，越大检测越松，一般范围在25到50之间；"));
    ui->text4_9->setToolTip(QString::fromLocal8Bit("外围缺陷面积: \n默认  50;\n配合参数 外围缺陷阈值 ，缺陷面积，越大检测越松；"));
    ui->text4_10->setToolTip(QString::fromLocal8Bit("裂痕阈值: \n默认  2;\n主要检测裂痕，越大越严格，一般不要超过10；"));
    ui->text4_11->setToolTip(QString::fromLocal8Bit("裂痕长度: \n默认  15;\n配合参数 裂痕阈值 进行检测，为裂痕的长度，越大检测越松，一般范围在5到40；"));
    ui->text4_12->setToolTip(QString::fromLocal8Bit("涂层阈值: \n默认  180;\n主要检测涂层缺损，为涂层亮度的阈值，越大越松，一般范围在160到220之间；"));
    ui->text4_13->setToolTip(QString::fromLocal8Bit("涂层面积: \n默认  500;\n结合参数 涂层阈值 进行检测，越大越松；"));
    ui->text4_14->setToolTip(QString::fromLocal8Bit("内孔阈值: \n默认  180;\n定位内孔阈值；"));
    ui->text4_15->setToolTip(QString::fromLocal8Bit("内孔缺陷面积: \n默认  20;\n内孔异物的面积，越大检测越松；"));
    ui->text4_16->setToolTip(QString::fromLocal8Bit("内孔内缩尺寸: \n默认  2;\n根据内孔检测园的半径，外内内缩尺寸，越大检测越松；"));
    ui->text4_17->setToolTip(QString::fromLocal8Bit("内孔最小尺寸: \n默认  30;\n检测内孔园的半径，小于这个直接判别废品；"));
    ui->text4_18->setToolTip(QString::fromLocal8Bit("台阶检测区域位置: \n默认  10;\n检测内孔沉台的缺陷，为在内孔半径基础上外扩尺寸，该尺寸数据过大会定位到沉台黑色区域，引起误检，太小则会检测内孔不需要判别的异物；"));
    ui->text4_19->setToolTip(QString::fromLocal8Bit("台阶检测区域宽度: \n默认  8;\n在参数 台阶检测区域位置 的基础上，检测范围，越大越严格；"));
    ui->text4_20->setToolTip(QString::fromLocal8Bit("台阶检测阈值: \n默认  35;\n沉台异物的黑色阈值，越大越严格；"));
    ui->text4_21->setToolTip(QString::fromLocal8Bit("台阶缺陷面积: \n默认  50;\n配合参数20，缺陷面积，越大越松；"));
    ui->text4_22->setToolTip(QString::fromLocal8Bit("是否存带标识图: \n0   不存图\n1   存缺陷标识图\n2   存不带标识图"));
}

void DetectionDlg::RedAlgPlaceIni()
{
    if (!m_bOpen){
        return;
    }

    QString Path = "Config/Product/" + m_dRoi->m_sIniFilePath;
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));


    HalconCpp::HTuple* fAlgPlaceA = &m_dRoi->m_algPlaceA.x1;
    HalconCpp::HTuple* fAlgPlaceB = &m_dRoi->m_algPlaceB.x1;



    for (int j = 0; j < 4; j++){
        *fAlgPlaceA = iniRead.value(QString("AlgPlaceA_%1/parame%2").arg(m_dRoi->m_nCCDNum).arg(j)).toFloat();
        fAlgPlaceA++;
    }

    for (int j = 0; j < 4; j++){
        *fAlgPlaceB = iniRead.value(QString("AlgPlaceB_%1/parame%2").arg(m_dRoi->m_nCCDNum).arg(j)).toFloat();
        fAlgPlaceB++;
    }

    m_dRoi->ui->EditRoiA_1->setText(QString::number(m_dRoi->m_algPlaceA.x1.D()));
    m_dRoi->ui->EditRoiA_2->setText(QString::number(m_dRoi->m_algPlaceA.y1.D()));
    m_dRoi->ui->EditRoiA_3->setText(QString::number(m_dRoi->m_algPlaceA.x2.D()));
    m_dRoi->ui->EditRoiA_4->setText(QString::number(m_dRoi->m_algPlaceA.y2.D()));


    m_dRoi->ui->EditRoiB_1->setText(QString::number(m_dRoi->m_algPlaceB.x1.D()));
    m_dRoi->ui->EditRoiB_2->setText(QString::number(m_dRoi->m_algPlaceB.y1.D()));
    m_dRoi->ui->EditRoiB_3->setText(QString::number(m_dRoi->m_algPlaceB.x2.D()));
    m_dRoi->ui->EditRoiB_4->setText(QString::number(m_dRoi->m_algPlaceB.y2.D()));

}
bool DetectionDlg::eventFilter(QObject *obj, QEvent *event){
    if (obj == ui->label)//指定某个QLabel
    {
        if (!m_bOpen)
        {
            return false;
        }
        if (event->type() == QEvent::MouseMove)//mouse button pressed
        {
            HalconCpp::HTuple grayval;

            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            QPoint p = mouseEvent->pos();
            // w 680 h510

            HalconCpp::HTuple w, h;
            HalconCpp::GetImageSize(Hobj, &w, &h);
            float y = float(h) / 510;
            float x = float(w) / 680;
            HalconCpp::GetGrayval(Hobj, p.y()*y, p.x()*x, &grayval);
            ui->line_65->setText(QString::number(grayval[0].I()));
            ui->line_66->setText(QString("%1,%2").arg(p.y()).arg(p.x()));
            return false;
        }
        if (event->type() == QEvent::Wheel){
            QWheelEvent * pWheel = (QWheelEvent *)event;
            part += pWheel->delta() / 5;
            //ShowImage(Hobj);
        }

    }
    else
    {
        // pass the event on to the parent class
        return DetectionDlg::eventFilter(obj, event);
    }
    return false;
}

void DetectionDlg::on_switchBt_1_clicked()
{
    emit changeDlg(1);
}

void DetectionDlg::on_switchBt_2_clicked()
{
    emit changeDlg(2);
}
void DetectionDlg::on_switchBt_3_clicked()
{
    emit changeDlg(3);
}

void DetectionDlg::on_switchBt_4_clicked()
{
    emit changeDlg(4);
}

void DetectionDlg::on_OpenFileBt_clicked()
{
    QString OpenFile;
    //打开文件夹中的图片文件
    OpenFile = QFileDialog::getOpenFileName(this,
                                            "Please choose an image file",
                                            "",
                                            "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");

    if (OpenFile != "")
    {
        //		if (!m_bOpen)
        //		{
        //			CameraList(1);
        //		}
        HalconOpenwindows();
        CarouselImage(OpenFile);
        // 获取当前文件所有图片名称
        QFileInfo fi;
        fi = QFileInfo(OpenFile);
        file_name = fi.fileName();
        file_path = fi.absolutePath();
        QDir dir(file_path);
        file_list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
        m_nImgIndex = file_list.indexOf(file_name);

    }
}

void DetectionDlg::on_camear_1_clicked()
{
    CameraList(1);
    ui->tabWidget->setCurrentIndex(0);
}

void DetectionDlg::on_camear_2_clicked()
{
    CameraList(2);
    ui->tabWidget->setCurrentIndex(1);
}

void DetectionDlg::on_camear_3_clicked()
{
    CameraList(3);
    ui->tabWidget->setCurrentIndex(2);
}

void DetectionDlg::on_camear_4_clicked()
{
    CameraList(4);
    ui->tabWidget->setCurrentIndex(3);
}

void DetectionDlg::CameraList(int nIndex)
{
    m_detectionInfo.m_nCamera = nIndex;

    m_dRoi->m_nCCDNum = nIndex;
    qDebug() << "DetectionDlg CameraList: " << nIndex;
    switch (nIndex) {
    case 0:
        ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
        ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
        ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
        ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");
    case 1:
        ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1B.png);");
        ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
        ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
        ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");
        break;
    case 2:
        ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2B.png);");
        ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
        ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
        ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");

        break;
    case 3:
        ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3B.png);");
        ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
        ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
        ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");

        break;
    case 4:
        ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4B.png);");
        ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
        ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
        ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
        break;
    default:
        break;
    }

    if (m_bOpen)
    {
        ShowImage(Hobj);
    }
    m_detectionInfo.m_nFunction = 1;
    RedAlgPlaceIni();
    emit sigDltoDetection(m_detectionInfo);
}

void DetectionDlg::HalconOpenwindows()
{
    if (!m_bOpen)
    {
        Hlong hWind = ui->label->winId();
        HalconCpp::OpenWindow(0, 0, ui->label->width(), ui->label->height(), hWind, "", "", &hv_WindowHandle);
        m_bOpen = true;
        m_dRoi->m_bOpen = m_bOpen;
        m_dRoi->hv_WindowHandle = hv_WindowHandle;
    }
}

void DetectionDlg::DetectionSize(HObject Hobj)
{
    m_AlgSize.para2 = m_detectionInfo.m_Cam2;

    m_AlgSize.m_placeA.x1 = m_dRoi->m_algPlaceA.x1;
    m_AlgSize.m_placeA.y1 = m_dRoi->m_algPlaceA.y1;
    m_AlgSize.m_placeA.x2 = m_dRoi->m_algPlaceA.x2;
    m_AlgSize.m_placeA.y2 = m_dRoi->m_algPlaceA.y2;
    m_AlgSize.m_placeA.hvCCDHandle = hv_WindowHandle;

    m_AlgSize.m_placeB.x1 = m_dRoi->m_algPlaceB.x1;
    m_AlgSize.m_placeB.y1 = m_dRoi->m_algPlaceB.y1;
    m_AlgSize.m_placeB.x2 = m_dRoi->m_algPlaceB.x2;
    m_AlgSize.m_placeB.y2 = m_dRoi->m_algPlaceB.y2;
    m_AlgSize.m_placeB.hvCCDHandle = hv_WindowHandle;
    m_nNGorOK = m_AlgSize.M_Length(Hobj, false, Len);
    ho_GrayImage = Hobj;
}

void DetectionDlg::DetectionCricle(HObject Hobj)
{
    HObject img;
    m_AlgCricle.para1 = m_detectionInfo.m_Cam1;


    m_AlgCricle.m_place.x1 = m_dRoi->m_algPlaceA.x1;
    m_AlgCricle.m_place.y1 = m_dRoi->m_algPlaceA.y1;
    m_AlgCricle.m_place.x2 = m_dRoi->m_algPlaceA.x2;
    m_AlgCricle.m_place.y2 = m_dRoi->m_algPlaceA.y2;
    m_AlgCricle.m_place.hvCCDHandle = hv_WindowHandle;

    m_nNGorOK = m_AlgCricle.M_Cricle(Hobj, false, In_Round, Out_Round, In_Len, Out_Len, img);
    ho_GrayImage = img;
}

void DetectionDlg::DetectionDefect(HObject Hobj, int nIndex){
    CCD_34_PARAME cCamParame;
    if (nIndex == 3){
        cCamParame = m_detectionInfo.m_Cam3;
    }
    else {
        cCamParame = m_detectionInfo.m_Cam4;
    }
    cCamParame.parame21 = 0;    // 检测不存图



    m_AlgDefect.m_place.x1 = m_dRoi->m_algPlaceA.x1;
    m_AlgDefect.m_place.y1 = m_dRoi->m_algPlaceA.y1;
    m_AlgDefect.m_place.x2 = m_dRoi->m_algPlaceA.x2;
    m_AlgDefect.m_place.y2 = m_dRoi->m_algPlaceA.y2;
    m_AlgDefect.m_place.hvCCDHandle = hv_WindowHandle;


    m_nNGorOK = m_AlgDefect.Defect_detect(Hobj, cCamParame, nIndex);
    ho_GrayImage = m_AlgDefect.m_Image_Show;
    //HalconCpp::Rgb1ToGray(m_AlgDefect.m_Image_Show, &ho_GrayImage);  // 彩色转 灰色图像  用于检测 灰度值 暂时舍弃
}

void DetectionDlg::on_writeBt_clicked()
{
    if (m_detectionInfo.m_nCamera == 0){
        return;
    }
    /* 修改int数据 和内存中的数据*/
    m_detectionInfo.m_nFunction = 2;
    emit sigDltoDetection(m_detectionInfo);
    CameraList(m_detectionInfo.m_nCamera);
}

void DetectionDlg::on_cameraBt_1_clicked()
{
    // 相机1标定
    m_nTest = 1 - m_nTest;
//    float m_cal1;
//    bool bCal = m_cal.Cal(1, 0, 0, m_cal1);
//    if (!bCal){
//        m_cal1 = 0;
//    }
//    ui->line_63->setText(QString::number(m_cal1));
}

void DetectionDlg::on_cameraBt_2_clicked()
{
    // 相机2标定
    float m_cal1 = 0;
    bool bCal = m_cal.Cal(2, m_detectionInfo.m_Cam2.Gap_Point, m_detectionInfo.m_Cam2.Glass_H, m_cal1);
    if (!bCal){
        m_cal1 = 0;;
    }
    ui->line_64->setText(QString::number(m_cal1));
}

void DetectionDlg::CarouselImage(QString OpenFile){
    QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题
    std::string name = code->fromUnicode(OpenFile).data();

    HalconCpp::ReadImage(&Hobj, name.c_str());

    m_dRoi->Hobj = Hobj;
    CameraList(m_detectionInfo.m_nCamera);
    ui->tabWidget->setCurrentIndex(m_detectionInfo.m_nCamera - 1);
    ui->line_67->setText(OpenFile);
}


void DetectionDlg::ShowImage(HObject img){
    HTuple w, h;
    GetImageSize(img, &w, &h);
//    set_part（startRow,startCol,endRow,EndCol）
    SetPart(hv_WindowHandle, 0, 0, h, w);
    DispObj(img, hv_WindowHandle);
}

void DetectionDlg::ShowImage(HObject img, HTuple hv_WindowHandle, int index)
{
    m_nTexHeight = 30;
    HTuple w, h;
    try
    {
        GetImageSize(img, &w, &h);
        SetPart(hv_WindowHandle, 0, 0, h, w);
        DispObj(img, hv_WindowHandle);
        switch (index) {
        case 1:
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(1050));
            m_nTexX = 30;
            m_nTexY = 1050;
            if (m_nNGorOK != 0)
            {
                SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK == 1)
				{
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}
            }
            else
            {
                SetColor(hv_WindowHandle, "green");
                WriteString(hv_WindowHandle, "OK");
            }
            if (m_AlgCricle.InRoundFlag)
            {
                SetColor(hv_WindowHandle, "green");
            }
            else
            {
                SetColor(hv_WindowHandle, "red");
            }
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
            WriteString(hv_WindowHandle, "内圆度=" + In_Round);
            if (m_AlgCricle.OutRoundFlag)
            {
                SetColor(hv_WindowHandle, "green");
            }
            else
            {
                SetColor(hv_WindowHandle, "red");
            }
            SetTposition(hv_WindowHandle, HTuple(150), HTuple(50));
            WriteString(hv_WindowHandle, "外圆度=" + Out_Round);
            if (m_AlgCricle.InlenFlag)
            {
                SetColor(hv_WindowHandle, "green");
            }
            else
            {
                SetColor(hv_WindowHandle, "red");
            }
            SetTposition(hv_WindowHandle, HTuple(270), HTuple(50));
            WriteString(hv_WindowHandle, "内直径=" + In_Len);
            if (m_AlgCricle.OutlenFlag)
            {
                SetColor(hv_WindowHandle, "green");
            }
            else
            {
                SetColor(hv_WindowHandle, "red");
            }
            SetTposition(hv_WindowHandle, HTuple(390), HTuple(50));
            WriteString(hv_WindowHandle, "外直径=" + Out_Len);
            if (m_AlgCricle.CricleFlag)
            {
                SetColor(hv_WindowHandle, "green");
            }
            else
            {
                SetColor(hv_WindowHandle, "red");
            }
            SetTposition(hv_WindowHandle, HTuple(510), HTuple(50));
            WriteString(hv_WindowHandle, "圆心距=" + m_AlgCricle.Distance);
            break;
        case 2:
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(2200));
            m_nTexX = 30;
            m_nTexY = 2200;
            if (m_nNGorOK != 0)
            {
                SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK == 1)
				{
					WriteString(hv_WindowHandle, "NG");
				}
				else {
					WriteString(hv_WindowHandle, "异常");
				}
            }
            else
            {
                SetColor(hv_WindowHandle, "green");
                WriteString(hv_WindowHandle, "OK");
            }
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(50));
            WriteString(hv_WindowHandle, "长度=" + Len);

            break;
        case 3:
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(700));
            m_nTexX = 30;
            m_nTexY = 700;
            if (m_nNGorOK != 0)
            {
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK ==1 )
				{
					WriteString(hv_WindowHandle, "NG");
				}
				else{
					WriteString(hv_WindowHandle, "异常");
				}
                
                

                if (m_AlgDefect.g_hole_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "内孔异物面积:" + m_AlgDefect.g_hole_max);
                }
                if (m_AlgDefect.g_hole_chengtaiyiwu_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "沉台缺陷面积:" + m_AlgDefect.g_hole_chengtaiyiwu_max);
                }
                if (m_AlgDefect.g_waiweimaoci_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "外围毛疵:" + m_AlgDefect.g_waiweimaoci_max);
                }
                if (m_AlgDefect.g_heidian_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "黑点缺陷面积:" + m_AlgDefect.g_heidian_max);
                }
                if (m_AlgDefect.g_waibianyuan_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "外边缘缺陷:" + m_AlgDefect.g_waibianyuan_max);
                }
                if (m_AlgDefect.g_tuceng_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "涂层面积:" + m_AlgDefect.g_tuceng_max);
                }
                if (m_AlgDefect.g_liehen_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "裂痕长度:" + m_AlgDefect.g_liehen_max);
                }
                if (m_AlgDefect.g_quexian_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "常规缺陷面积:" + m_AlgDefect.g_quexian_max);
                }

            }
            else
            {
                SetColor(hv_WindowHandle, "green");
                WriteString(hv_WindowHandle, "OK");
            }


            break;
        case 4:
            SetTposition(hv_WindowHandle, HTuple(30), HTuple(700));
            m_nTexX = 30;
            m_nTexY = 700;
            if (m_nNGorOK != 0)
            {
				SetColor(hv_WindowHandle, "red");
				if (m_nNGorOK == 1)
				{
					WriteString(hv_WindowHandle, "NG");
				}
				else{
					WriteString(hv_WindowHandle, "异常");
				}

                if (m_AlgDefect.g_hole_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "内孔异物面积:" + m_AlgDefect.g_hole_max);
                }
                if (m_AlgDefect.g_hole_chengtaiyiwu_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "沉台缺陷面积:" + m_AlgDefect.g_hole_chengtaiyiwu_max);
                }
                if (m_AlgDefect.g_waiweimaoci_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "外围毛疵:" + m_AlgDefect.g_waiweimaoci_max);
                }
                if (m_AlgDefect.g_heidian_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "黑点缺陷面积:" + m_AlgDefect.g_heidian_max);
                }
                if (m_AlgDefect.g_waibianyuan_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "外边缘缺陷:" + m_AlgDefect.g_waibianyuan_max);
                }
                if (m_AlgDefect.g_tuceng_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "涂层面积:" + m_AlgDefect.g_tuceng_max);
                }
                if (m_AlgDefect.g_liehen_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "裂痕长度:" + m_AlgDefect.g_liehen_max);
                }
                if (m_AlgDefect.g_quexian_max > 0){
                    m_nTexHeight += 30;
                    SetTposition(hv_WindowHandle, HTuple(m_nTexHeight), HTuple(50));
                    WriteString(hv_WindowHandle, "常规缺陷面积:" + m_AlgDefect.g_quexian_max);
                }
            }
            else
            {
                SetColor(hv_WindowHandle, "green");
                WriteString(hv_WindowHandle, "OK");
            }
            break;
        default:
            break;
        }




        HObject Rect, Contour;

        // 显示检测Roi区域
        HalconCpp::GenRectangle1(&Rect, m_dRoi->m_algPlaceA.x1, m_dRoi->m_algPlaceA.y1, m_dRoi->m_algPlaceA.x2, m_dRoi->m_algPlaceA.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);


        HalconCpp::GenRectangle1(&Rect, m_dRoi->m_algPlaceB.x1, m_dRoi->m_algPlaceB.y1, m_dRoi->m_algPlaceB.x2, m_dRoi->m_algPlaceB.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);

        SetTposition(hv_WindowHandle, HTuple(m_nTexX+50), HTuple(m_nTexY - 80));
        WriteString(hv_WindowHandle, msg.toStdString().c_str());


    }
    catch (HException &exception)
    {
    }
}

void DetectionDlg::on_clearBt_clicked()
{
    if (QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("以下操作会清除所有图片！"), tr("Yes"), tr("No")))
    {
        return;
    }
    QString imgPath = QDir::currentPath() + "/Pic";
    deleteDir(imgPath);
    QMessageBox::information(this, QString::fromLocal8Bit("成功"), QString::fromLocal8Bit("所有图片已经清除完成！"));
}


//删除文件夹
bool DetectionDlg::deleteDir(const QString &path)
{

    if (path.isEmpty()){
        return false;
    }
    QDir dir(path);
    if (!dir.exists()){
        return true;
    }
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); //设置过滤
    QFileInfoList fileList = dir.entryInfoList(); // 获取所有的文件信息
    foreach(QFileInfo file, fileList){ //遍历文件信息
        if (file.isFile()){ // 是文件，删除
            file.dir().remove(file.fileName());
        }
        else{ // 递归删除
            deleteDir(file.absoluteFilePath());
        }
    }
    return true;
}

void DetectionDlg::on_upBt_clicked()
{
    if (file_list.empty()){
        return;
    }
    QString OpenFile;
    m_nImgIndex--;
    if (m_nImgIndex == -1){
        m_nImgIndex = file_list.size() - 1;
    }
    OpenFile = QString("%1/%2").arg(file_path).arg(file_list[m_nImgIndex]);
    CarouselImage(OpenFile);
}

void DetectionDlg::on_downBt_clicked()
{
    if (file_list.empty()){
        return;
    }
    QString OpenFile;
    m_nImgIndex++;
    if (m_nImgIndex == file_list.size()){
        m_nImgIndex = 0;
    }
    OpenFile = QString("%1/%2").arg(file_path).arg(file_list[m_nImgIndex]);
    CarouselImage(OpenFile);
}

void DetectionDlg::on_staticBt_clicked()
{
    if(m_bOpen){
        QTime time;
        time.start();
        switch (m_detectionInfo.m_nCamera) {
        case 1:
            DetectionCricle(Hobj);
            break;
        case 2:
            DetectionSize(Hobj);
            break;
        case 3:
            DetectionDefect(Hobj, 3);
            break;
        case 4:
            DetectionDefect(Hobj, 4);
            break;
        }
        msg = QString("Time:%1s").arg(time.elapsed() / 1000.0);
        ShowImage(ho_GrayImage, hv_WindowHandle, m_detectionInfo.m_nCamera);
    }
}

void DetectionDlg::on_areaBt_clicked()
{
    // 计算面积
    if (!m_bOpen)
    {
        return;
    }
    HTuple In_Area, Out_Area;
    m_AlgArea.m_area(Hobj, In_Area, Out_Area);

    ui->cm1_15->setText(QString::number(Out_Area.D()));
    ui->cm1_16->setText(QString::number(In_Area.D()));
}

void DetectionDlg::on_SetRoiBt_clicked()
{
    // 打开Roi设置页面
    m_dRoi->show();
}
